package ca.cbc.msb.persistence;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import ca.cbc.medialib.dao.MediaDAO;
import ca.cbc.medialib.entity.Media;

public class MediaPersistence {
	private MediaDAO mediaDAO;
	
	public MediaDAO getMediaDAO() {
		return mediaDAO;
	}
	
	public void setMediaDAO(MediaDAO mediaDAO) {
		this.mediaDAO = mediaDAO;
	}
	
	@Transactional (readOnly=true)
	public Media findById(long releaseId) {
		return mediaDAO.findById(releaseId);
	}
	
	@Transactional
	public void delete(Media media) {
		if(media == null) return;
		if(findById(media.getReleaseId()) != null) {
			mediaDAO.delete(media);
		}
	}
	
	@Transactional
	public void saveOrMerge(Media media) {
		if(media == null) return;
		if(findById(media.getReleaseId()) == null) {
			mediaDAO.save(media);
		} else {
			mediaDAO.merge(media);
		}
	}
	
	@Transactional
	public List<Media> listExpiredMobileMedia(int first, int max) {
		return mediaDAO.listExpiredMobileMedia(first, max);
	}
}
